package thread;

// 完成abc这样子打印 进行十次而且有三个线程分别为abc
//一把锁解决问题
public class ThreadDemo18 {
    private static int count = 0;
    public static void main(String[] args) {
        Object locker = new Object();

        Thread a = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count % 3 != 0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        Thread b = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count % 3 != 1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        Thread c = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (locker){
                    while(count % 3 != 2){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    count++;
                    locker.notifyAll();
                }
            }
        });

        a.start();
        b.start();
        c.start();

    }
}
